#include <bits/stdc++.h>
using namespace std;

unordered_map<char, int> ctoi = {
    {'0', 0}, {'1', 1}, {'2', 2},
    {'3', 3}, {'4', 4}, {'5', 5},
    {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9},
    {'a', 10}, {'b', 11}, {'c', 12},
    {'d', 13}, {'e', 14}, {'f', 15}
};



int main(){
    int n;
    cin >> n;
    while(n--){
        int B;
        string str;
        cin >> B >> str;
        int len = str.size(), sum = 0;
        for(int i = 0; i < len; ++i){
            sum += ctoi[str[i]];
        }
        int ans = B - 1 - (sum % (B - 1));
        if(ans == B - 1) cout << 0 << endl;
        else if(ans < 10) cout << ans << endl;
        else cout << (char)('a' + ans - 10) << endl;
    }
    system("pause");
}